import { defineStore } from "pinia";

export interface Task {
    name: string,
    isComplete: boolean
}
export interface TaskState {
    tasks: Task[]
}
export const updateTaskStore = defineStore('updateTask',{
    state: (): TaskState => ({
        tasks: [
            {
              name: '测试',
              isComplete: false
            },
            {
                name: '吹葫芦丝',
                isComplete: false
            },
            {
                name: '敲代码',
                isComplete: true
            }
          ]
    }),

    getters: {
        getTasks() {
            return this.tasks;
        },
    },

    actions: {
        // 增加任务
        addTask(task: Task) {
            this.tasks.push(task);
        },
        delTask(index:number) {
            this.tasks.splice(index,1);
        },
        updateTask(index:number,isComplete:boolean) {
            this.tasks[index].isComplete = isComplete;
        },
        // 清除已完成
        clearCompletedTask() {
            
            this.tasks.splice(this.tasks.findIndex(item=> item.isComplete === true),1);
            console.log('清除已完成的');
        },
        setTasks(tasks:TaskState) {
            this.tasks = tasks;
        }
    }
})